package com.eat.controller.user;

import com.eat.dto.OrdersPaymentDTO;
import com.eat.dto.OrdersSubmitDTO;
import com.eat.entity.Orders;
import com.eat.result.PageResult;
import com.eat.result.Result;
import com.eat.service.OrderService;
import com.eat.vo.OrderPaymentVO;
import com.eat.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     *
     * @param submitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO submitDTO) {

        OrderSubmitVO submitVO = orderService.submitOrder(submitDTO);

        return Result.success(submitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
//        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        OrderPaymentVO orderPaymentVO =  OrderPaymentVO.builder()
                .nonceStr(String.valueOf(System.currentTimeMillis()))
                .paySign("签名")
                .packageStr(String.valueOf(LocalDateTime.now()))
                .signType("aa")
                .timeStamp("123")
                .build();
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);

    }

    /**
     * 查询详细订单信息
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    public Result<Orders> getOrderDetail(@PathVariable Long id){
        log.info("查询订单:");
        Orders orders =  orderService.getOrder(id);
        return Result.success(orders);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    public Result quitOrder(@PathVariable Long id){
        log.info("取消订单:");

        orderService.delete(id);

        return Result.success();
    }

    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/historyOrders")
    public Result<PageResult> page(Integer page,Integer pageSize,Integer status){
        log.info("{},{},{}",page,pageSize,status);
        PageResult pageResult =  orderService.page(page,pageSize,status);

        return Result.success(pageResult);
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result againOrder(@PathVariable Long id){

        orderService.againOrder(id);

        return Result.success();
    }

    /**
     * 催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    public Result reminderOrder(@PathVariable Long id){

        orderService.reminderOrder(id);

        return Result.success();
    }
}
